namespace :db do
  desc "Fill database with sample data"
  task :populate => :environment do
    #Rake::Task['db:reset'].invoke
    make_users
#    make_categories
#    make_products
  end
end

def make_users
  admin = User.create!(:firstname => "Example",
                       :lastname => "User",
                       :email => "railsshop@gmail.com",
                       :password => "foobar",
                       :password_confirmation => "foobar")
  admin.toggle!(:admin)

  title_array = ["Frau", "Herr", "Firma"]

  99.times do |n|
    firstname = Faker::Name.first_name
    lastname = Faker::Name.last_name
    email = Faker::Internet.email(firstname + lastname)
    password = "password"
    User.create!(:firstname => firstname,
                 :lastname => lastname,
                 :email => email,
                 :password => password,
                 :password_confirmation => password,
                 :title => title_array[rand(2)] ,
                 :street => Faker::Address.street_address,
                 :zip => Faker::Address.zip_code,
                 :city => Faker::Address.city)
  end
end

def make_categories
  9.times do |n|
    name = Faker::Company.name
    Category.create!(:name => name)
  end
end

def make_products
  Category.all.each do |category|
    50.times do
      name = "Produkt #{rand(1000000)}"
      description = Faker::Lorem.sentence(3)
      price = rand(1000)
      category_id = category.id

      Product.create!(:name => name,
                      :description => description,
                      :price => price,
                      :category_id => category_id)
    end
  end
end